package com.piece.core.web.filter;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 上下文过滤器
 */
@Slf4j
public class ContextFilter extends OncePerRequestFilter {

    private FilterHookManager filterHookManager;

    public ContextFilter(FilterHookManager filterHookManager) {
        this.filterHookManager = filterHookManager;
    }

    @Override
    public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        Pair pair = this.filterHookManager.beforeFilter(request, response);
        chain.doFilter((ServletRequest) pair.getLeft(), (ServletResponse) pair.getRight());
        this.filterHookManager.afterFilter(request, response);
    }
}
